//app.js
import {
    getUserInfo,
    getUserAddrs,
    getCardInfo,
    getSchool
} from './utils/apis'
import {
    getCurrentAddress,
    coordFormat
} from './utils/util'
import {
    gcj02tobd09
} from './utils/coordtransform'
import distance from './utils/distance'

const {
    $Toast
} = require('./dist/base/index');


App({
    onLaunch: function() {
        // 注册默认地址
        this.saveDefaultAddress();
        // 注册学校信息
        this.getSchool();
    },
    getLoginInfo: function(save) {
        if (!this.globalData.loginInfo) {
            //先从缓存中取
            var info = wx.getStorageSync('userInfo');
            if (info) {
                save(info);
                return;
            }
            // 缓存不存在 登录操作
            wx.login({
                success(res) {
                    if (res.code) {
                        getUserInfo({
                            code: res.code,
                            success: (response) => {
                                save(response);
                            }
                        })
                    } else {
                        $Toast({
                            content: '网络错误',
                            type: 'warning'
                        });
                    }
                }
            });
        }
    },
    setLoginInfo(loginInfo) {
        if (loginInfo) {
            wx.setStorageSync('userInfo', loginInfo);
            this.globalData.loginInfo = loginInfo;
            console.log(this.userInfoCallback);
            if (this.userInfoCallback) {
                this.userInfoCallback(loginInfo);
            }
        }
    },
    getSchool(){
        var that=this;
        getSchool({
            data:{},
            success:function(schools){
                that.globalData.schoolList=schools;
            }
        });
    },
    saveDefaultAddress() {
        var address = wx.getStorageSync('address');
        if (!address || JSON.stringify(address)==='{}') {
            wx.setStorageSync('address', {
                id: 1,
                name: "西北大学"
            });
        }
    },
    globalData: {
        loginInfo: null,
        cardInfo: {
            startTime: (new Date()).getTime() - 7200,
            endTime: (new Date()).getTime() - 7200,
            id: 0
        },
        openCode: null,
        wxUserInfo: null,
        schoolList:null
    }
});